<?php
include("include/config.inc.php");
include('../thumbnailClass.php');
if(!isset($_SESSION['s_activIdsAdmin']) && !isset($_SESSION['s_activIdsAdmin2']))
{
  header("Location:checkLogin.php");
}
else
{
  if(isset($_REQUEST['submit']))
  {      
    $userId         = isset($_POST['userId']) ? $_POST['userId'] : 0;
    $contractId     = isset($_POST['contractId']) ? $_POST['contractId'] : 0;
    $invoiceId      = isset($_POST['invoiceId']) ? $_POST['invoiceId'] : 0;
    $userIdAdmin    = isset($_SESSION['s_activIdsAdmin']) ? ($_SESSION['s_activIdsAdmin']) : ($_SESSION['s_activIdsAdmin2']);
    $uploadDateTime = date('Y-m-d h:i:s');
    
    for($i=0;$i<count($_FILES["uploadFile"]["name"]);$i++)  
    {
      if(trim($_FILES["uploadFile"]["tmp_name"][$i]) != "")  
      {
        $insertUserDoc = "INSERT INTO userdoc (userId,contractId,invoiceId,uploadedBy,displayFileName,uploadDateTime)
                          VALUES (".$userId.",".$contractId.",".$invoiceId.",".$userIdAdmin.",'".$_FILES["uploadFile"]["name"][$i]."','".$uploadDateTime."')";
        $insertUserDocRes = mysql_query($insertUserDoc);
        $lastUserDocId = mysql_insert_id();
        
	      $images = $_FILES["uploadFile"]["tmp_name"][$i];

	      $new_images = "".$lastUserDocId."_".$i."_".$_FILES["uploadFile"]["name"][$i];  
	      copy($_FILES["uploadFile"]["tmp_name"][$i],"../uploadDoc/".$new_images);

	      $strSQL = "UPDATE userdoc SET uploadFileName = '".$new_images."' WHERE userDocId = ".$lastUserDocId."";
	      $objQuery = mysql_query($strSQL);
	      if(!$insertUserDocRes)
        {
          die ("Insert Query Not Insertd :" .mysql_error());    
        }
        else
        {
          header("Location:uploadPic.php");
        }
      }
    }
    header("Location:uploadPic.php");
  }
  $u=0;
  $userArray = array();
  $selectUser = "SELECT userId,fName
                   FROM user
                  WHERE userType = 'user'";
  $selectUserRes = mysql_query($selectUser);
  while($userRow = mysql_fetch_array($selectUserRes))
  {
    $userArray['userId'][$u] = $userRow['userId'];
    $userArray['fName'][$u]  = $userRow['fName'];
    $u++;
  }

  $o=0;
  $contractArray = array();
  $selectContract = "SELECT contractId,contractNo
                       FROM contract";
  $selectContractRes = mysql_query($selectContract);
  while($contractRow = mysql_fetch_array($selectContractRes))
  {
    $contractArray['contractId'][$o] = $contractRow['contractId'];
    $contractArray['contractNo'][$o] = $contractRow['contractNo'];
    $o++;
  }

  $l=0;
  $invoiceArray = array();
  $selectInvoice = "SELECT invoiceId,invoiceNo
                      FROM invoice";
  $selectInvoiceRes = mysql_query($selectInvoice);
  while($invoiceRow = mysql_fetch_array($selectInvoiceRes))
  {
    $invoiceArray['invoiceId'][$l] = $invoiceRow['invoiceId'];
    $invoiceArray['invoiceNo'][$l] = $invoiceRow['invoiceNo'];
    $l++;
  }
  include("bottom.php");
  $smarty->assign("invoiceArray",$invoiceArray);
  $smarty->assign("contractArray",$contractArray);
  $smarty->assign("userArray",$userArray);
  $smarty->display("uploadPic.tpl");
}
?>